package cn.hamster3.hamsterworld.command;

import cn.hamster3.hamsterworld.core.DataManager;
import cn.hamster3.hamsterworld.core.Message;
import cn.hamster3.hamsterworld.data.WorldData;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class InfoCommand extends CommandBase {
    public InfoCommand(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public boolean isPlayerCommand() {
        return false;
    }

    @Override
    public String getPermission() {
        return "hw.admin";
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length < 2) {
            sender.sendMessage(Message.notInputWorldName.toString());
            return true;
        }
        WorldData worldData = dataManager.getWorldData(args[1]);
        if (worldData == null) {
            sender.sendMessage(Message.worldNotExist.toString());
            return true;
        }
        StringBuilder builder = new StringBuilder();
        List<UUID> players = worldData.getAllowedPlayer();
        for (int i = 0; i < players.size(); i++) {
            builder.append(Bukkit.getOfflinePlayer(players.get(i)).getName());
            if (i != players.size() - 1) {
                builder.append(" ,");
            }
        }
        sender.sendMessage("§a======================[ 世界信息 ]======================");
        sender.sendMessage("§a世界名称: " + worldData.getName());
        sender.sendMessage("§aUUID: " + worldData.getUuid().toString());
        sender.sendMessage("§a世界主人: " + Bukkit.getOfflinePlayer(worldData.getOwner()).getName());
        sender.sendMessage("§a世界种子: " + worldData.getSeed());
        sender.sendMessage("§a世界大小: " + worldData.getBorderSize());
        sender.sendMessage("§a真实时间: " + worldData.isRealTime());
        sender.sendMessage("§a公共访问: " + worldData.isPublic());
        sender.sendMessage("§a好友名单: " + builder.toString());
        sender.sendMessage("§a======================[ 世界信息 ]======================");
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        ArrayList<String> tab = new ArrayList<>();
        if (!sender.hasPermission("hw.admin")) {
            return tab;
        }
        for (WorldData data : dataManager.getWorldData()) {
            if (!data.getName().toLowerCase().startsWith(args[1].toLowerCase())) {
                continue;
            }
            tab.add(data.getName());
        }
        return tab;
    }
}
